package org.ss.psci.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.ss.psci.entity.PmCompany;
import org.ss.psci.entity.PmProjects;
import org.ss.psci.entity.PmUser;
import org.ss.psci.service.ProjectService;
import org.ss.psci.service.UserService;

public class HomePageController extends MultiActionController {
	private String mainView;
	private String desktopView;
	private String errorView;
	private UserService userService;
	private ProjectService projectService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getErrorView() {
		return errorView;
	}

	public void setErrorView(String errorView) {
		this.errorView = errorView;
	}

	public String getDesktopView() {
		return desktopView;
	}

	public void setDesktopView(String desktopView) {
		this.desktopView = desktopView;
	}

	public String getMainView() {
		return mainView;
	}

	public void setMainView(String mainView) {
		this.mainView = mainView;
	}

	public ModelAndView showHomepage(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String projectsId = request.getParameter("projectsId");
		System.out.println("projectsId=" + projectsId);
		// if (username != null) {
		// if (!"".equals(username)) {
		// if ("basin".equals(username)) {
		// if (pwd != null) {
		// if (!"".equals(pwd)) {
		// if ("AGBHISGF".equals(pwd)) {
		// return new ModelAndView(this.getMainView());
		// }
		// }
		// }
		// }
		// }
		// }
		// return showError(request, response, command);
		return new ModelAndView(this.getMainView());
	}

	public ModelAndView showChooseConvert(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String username = request.getSession().getAttribute("username")
		.toString();
		System.out.println("showChooseConvert=>username=" + username);
		return new ModelAndView("/choose");
	}
	
	public ModelAndView showChoose(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String username = request.getParameter("username");
		request.getSession().setAttribute("username", username);
		PmUser pu = userService.getUserinfo(username);
		String companyId = pu.getCompanyId();
		request.getSession().setAttribute("companyId", companyId);
		request.getSession().setAttribute("userInfo", pu);
		PmCompany pc=userService.getCompanyinfo(companyId);
		request.getSession().setAttribute("companyName", pc.getName());
		request.getSession().setAttribute("companyInfo", pc);
		System.out.println("showChoose=>companyId=" + companyId);

		return new ModelAndView("/choose");
	}

	public ModelAndView showChooseProjects(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String username = request.getSession().getAttribute("username")
				.toString();
		String companyId = request.getSession().getAttribute("companyId")
		.toString();

		List projects = projectService.getPmProjectByCompanyID(companyId);
		List list = new ArrayList();

		System.out.println("showChooseProjects=>companyId=" + companyId + list.size());
		for (int i = 0; i < projects.size(); i++) {
			PmProjects p = (PmProjects) projects.get(i);
			Map map = new HashMap();
			map.put("companyId", p.getCompanyId());
			map.put("projectId", p.getProjectId());
			map.put("projectName", p.getName());
			System.out.println("projectName=" + p.getName() + " companyId="
					+ p.getCompanyId() + " projectId=" + p.getProjectId());
			list.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
		response.setContentType("text/html");
		response.setCharacterEncoding("gb2312");
		response.getWriter().write(jsonArray.toString());
		
		return null;
	}

	public ModelAndView showNewContract(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/newContractInfo");
	}

	public ModelAndView showNewAssignment(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/newAssignment");
	}

	public ModelAndView showNewProject(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/newProjectInfo");
	}

	public ModelAndView showNewBid(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/newBid");
	}

	public ModelAndView showDesktop(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String projectsId = request.getParameter("projectsId");
		if(projectsId==null){
			return new ModelAndView("/choose");
		}
		request.getSession().setAttribute("projectsId", projectsId);
		PmProjects projects = projectService.getPmProjectByProjectsId(projectsId);
		if(projects==null){
			return new ModelAndView("/choose");
		}
		request.getSession().setAttribute("projectInfo", projects);
		request.getSession().setAttribute("projectName", projects.getName());			
		System.out.println("projectsId=" + projectsId);
		return new ModelAndView(this.getDesktopView());
	}

	public ModelAndView showError(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView(this.getErrorView());
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

}
